package cn.ggb.eduservice.controller;


import cn.ggb.commonutils.OrderVo.EduCourseVo;
import cn.ggb.commonutils.R;
import cn.ggb.eduservice.entity.EduCourse;
import cn.ggb.eduservice.entity.vo.CourseInfoVo;
import cn.ggb.eduservice.entity.vo.CoursePublishVo;
import cn.ggb.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;
    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
      CourseInfoVo courseInfoVo =  eduCourseService.getCourseInfo(courseId);
      return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.PublishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }
    @PostMapping("publicCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //查询所有课程列表
    @GetMapping("findAll")
    public R findAllCourse(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    //实现分页的方法
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable long current,
                            @PathVariable long limit){
        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        //调用分页的方法
        eduCourseService.page(pageCourse,null);
        //查询总记录是
        long total = pageCourse.getTotal();
        //每页显示的记录
        List<EduCourse> rows = pageCourse.getRecords();
        return R.ok().data("total",total).data("rows",rows);
    }

    //实现条件分页查询
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageCourseQuery(@RequestBody EduCourse eduCourse,
                             @PathVariable long current,
                             @PathVariable long limit){
        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //动态sql条件
        String title = eduCourse.getTitle();
        String status = eduCourse.getStatus();
        //判断条件是否为空，如果不为空就进行拼接
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        //调用分页条件查询的方法
        eduCourseService.page(pageCourse,wrapper);
        //查询总记录是
        long total = pageCourse.getTotal();
        //每页显示的记录
            List<EduCourse> rows = pageCourse.getRecords();
        return R.ok().data("total",total).data("rows",rows);
    }

    //删除课程
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourseById(courseId);
        return R.ok();
    }



}

